package com.sqx.modules.health.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.AppPrescription;
import com.sqx.modules.health.service.IAppPrescriptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 医生药方参考表 前端控制器
 * </p>
 *
 * @author hsx
 * @since 2022-08-29
 */

@Api(value = "医生药方参考表", tags = "", description = "")
@RestController
@RequestMapping("/health/app/prescription")
public class AppPrescriptionController {
    @Autowired
    private IAppPrescriptionService appPrescriptionService;

    // 新增或更新
    @PostMapping("/save")
    @ApiOperation(value = "新增或更新医生药方参考表", notes = "新增或更新医生药方参考表")
    public Result save(@RequestBody AppPrescription appPrescription){
        System.out.println(appPrescription.toString());
        appPrescription.setCreatetime(LocalDateTime.now());
        appPrescription.setIsdelete(0);
        return Result.success(appPrescriptionService.saveOrUpdate(appPrescription));
    }

    // 根据ID删除
    @GetMapping("/del/{id}")
    @ApiOperation(value = "根据ID删除医生药方参考表", notes = "根据ID删除医生药方参考表")
    public Result delete(@PathVariable Integer id){
        AppPrescription update = appPrescriptionService.getById(id);
        update.setIsdelete(1);
        return Result.success(appPrescriptionService.saveOrUpdate(update));
    }

    // 根据IDS删除
    @PostMapping("/del/batch")
    @ApiOperation(value = "根据IDS删除医生药方参考表", notes = "根据IDS删除医生药方参考表")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(appPrescriptionService.removeByIds(ids));
    }

    // 查询全部
    @GetMapping("/findAll")
    @ApiOperation(value = "查询全部医生药方参考表", notes = "查询全部医生药方参考表")
    public Result findAll(){
        return Result.success(appPrescriptionService.list());
    }

    // 根据ID查询一条记录
    @GetMapping("/findOne")
    @ApiOperation(value = "根据ID查询一条记录医生药方参考表", notes = "根据ID查询一条记录医生药方参考表")
    public Result findOne(@PathVariable Integer id){
        return Result.success(appPrescriptionService.getById(id));
    }

    // 分页查询
    @GetMapping("/findPage")
    @ApiOperation(value = "分页查询医生药方参考表", notes = "分页查询医生药方参考表")
    public Result findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String searchName){
        QueryWrapper<AppPrescription> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isdelete", 0)
                .like("name", searchName)
                .orderByAsc("id");
        return  Result.success(appPrescriptionService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }
    // 条件查询
    @GetMapping("/findConditionPage")
    @ApiOperation(value = "条件查询医生药方参考表", notes = "条件查询医生药方参考表")
    public Result findConditionPage(@RequestParam Map<String, String> params){
        List ids = Arrays.asList(params.get("ids").split(","));
        QueryWrapper<AppPrescription> queryWrapper = new QueryWrapper<>();
        queryWrapper//.in("id", ids)
                .eq(ObjectUtil.isNotEmpty(params.get("type")), "type", params.get("type"))
                .eq("isdelete", 0);

        queryWrapper.orderByAsc("id");
        List<AppPrescription> getData =(List<AppPrescription>)appPrescriptionService.list(queryWrapper);
        return  Result.success(getData);
    }

    // 新增或更新
    @PostMapping("/savePrescription")
    @ApiOperation(value = "新增或更新医生药方参考表", notes = "新增或更新医生药方参考表")
    public Result savePrescription(@RequestBody AppPrescription appPrescription){
        return Result.success(appPrescriptionService.save(appPrescription));
    }
}

